/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interpreter.Function;

import Interpreter.InterpreterVisitor;
import ast.AST;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Stephen
 */
public class LengthFunction extends Function {
    @Override
    public Object execute(ArrayList args) {
        Object var = ((AST)args.get(0)).accept(new InterpreterVisitor());
        try {
            if (var.getClass() == Class.forName("java.lang.String")) {
                return Integer.toString(((String)var).length());
            } else if (var.getClass() == Class.forName("java.util.ArrayList")) {
                return Integer.toString(((ArrayList)var).size());
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LengthFunction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "0";
    }
}
